﻿module game.fontmanager;

import gamelib.graphics.renderer2d.renderer;
public import gamelib.graphics.renderer2d.font;
import gamelib.graphics.renderer2d.vectorfont;

class FontManagerBase(alias FontsList)
{
private:
    Renderer mRenderer;
    BaseFont[string] mFonts;
    
    static bool contains(in string str) pure nothrow
    {
        foreach(s;FontsList)
        {
            if(s == str) return true;
        }
        return false;
    }
public:
    this(Renderer ren) pure nothrow
    {
        mRenderer = ren;
    }
    
    ~this() pure nothrow
    {
        assert(mFonts.length == 0);
        assert(mRenderer is null);
    }
    
    void load()
    {
        import gamelib.util: TupleRange;
        foreach(i;TupleRange!(0,FontsList.length))
        {
            enum str = FontsList[i];
            mixin("import fnt="~str~";");
            mFonts[str] = new VectorFont(mRenderer, fnt.FontHeight, fnt.GlyphsDesc, fnt.GlyphsData, 0.5);
        }
    }

    void dispose()
    {

        foreach(f;mFonts)
        {
            assert(f !is null);
            f.dispose();
        }

        mFonts = mFonts.init;
        mRenderer = null;
    }
    
    @property auto font(string str)() pure
    {
        static assert(contains(str), "Font \""~str~"\" not in list");
        assert(mRenderer !is null);
        auto f = mFonts.get(str, null);
        assert(f !is null);
        return f;
    }
}
